package com.wanger.swa.controller;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wanger.swc.model.Result;
import com.wanger.swc.util.template.FileData;
import com.wanger.swa.domain.myEnum.BuildRecordEnum;
import com.wanger.swa.dto.buildRecord.query.BuildRecordQuery;
import com.wanger.swa.dto.buildRecord.req.model.BuildReq;
import com.wanger.swa.dto.buildRecord.req.BuildRecordAddReq;
import com.wanger.swa.dto.buildRecord.req.BuildRecordUpdateReq;
import com.wanger.swa.dto.buildRecord.resp.BuildRecordResp;
import com.wanger.swa.service.ise.BuildRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

/**
 * @Author: wanger
 * @Date: 2024/9/15 12:52
 * @Description: 构建记录
 */
@RestController
@Api(tags = "构建记录")
@RequiredArgsConstructor
@RequestMapping("/buildRecord")
public class BuildRecordController {

    private final BuildRecordService buildRecordService;

    @ApiOperation("构建后预览")
    @PostMapping("/buildPreview")
    public Result<FileData> buildPreview(@RequestBody BuildReq addDTO) {
        return Result.ok(buildRecordService.buildPreview(addDTO));
    }

    @ApiOperation("构建后压缩")
    @PostMapping("/buildZip")
    public void buildZip(@RequestBody BuildReq bu, HttpServletResponse response) throws IOException {
        OutputStream out = response.getOutputStream();
        response.reset();
        response.setContentType("application/octet-stream;charset=UTF-8");
        String filename = DateUtil.format(DateUtil.date(), DatePattern.PURE_DATETIME_MS_PATTERN) + "_sw.zip";
        response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + filename + "\"");
        buildRecordService.buildZip(bu, out);
    }

    @ApiOperation("添加构建记录")
    @PostMapping("/addBuildRecord")
    public Result<BuildRecordResp> addBuildRecord(@RequestBody BuildRecordAddReq addDTO) {
        return Result.ok(buildRecordService.addBuildRecord(addDTO));
    }

    @ApiOperation("修改构建记录")
    @PutMapping("/updateBuildRecord")
    public Result<BuildRecordResp> updateBuildRecord(@RequestBody BuildRecordUpdateReq updateDTO) {
        return Result.ok(buildRecordService.updateBuildRecord(updateDTO));
    }

    @ApiOperation("构建记录详情")
    @GetMapping("/getOneBuildRecord")
    public Result<BuildRecordResp> getOneBuildRecord(String id) {
        return Result.ok(buildRecordService.getOneBuildRecord(id));
    }

    @ApiOperation("分页查询构建记录")
    @GetMapping("/pageBuildRecord")
    public Result<IPage<BuildRecordResp>> pageBuildRecord(BuildRecordQuery query) {
        return Result.ok(buildRecordService.pageBuildRecord(query));
    }

    @ApiOperation("构建记录列表查询")
    @GetMapping("/listBuildRecord")
    public Result<List<BuildRecordResp>> listBuildRecord(BuildRecordQuery query) {
        return Result.ok(buildRecordService.listBuildRecord(query));
    }

    @ApiOperation("删除构建记录")
    @DeleteMapping("/deleteBuildRecord")
    public Result<Void> deleteBuildRecord(String id) {
        buildRecordService.deleteBuildRecord(id);
        return Result.ok();
    }

    @ApiOperation("构建记录枚举")
    @GetMapping("/getEnumBuildRecord")
    public Result<Map<String, List<Map<String, String>>>> getEnumBuildRecord() {
        return Result.ok(BuildRecordEnum.getList());
    }
}
